package com.unicom.shenzhen.sgip.impl;

import com.cloudhopper.commons.util.HexUtil;
import com.cloudhopper.commons.util.windowing.WindowFuture;
import com.unicom.shenzhen.sgip.SgipAsyncPduResponse;
import com.unicom.shenzhen.sgip.pdu.SgipPduRequest;
import com.unicom.shenzhen.sgip.pdu.SgipPduResponse;

/**
 * Created by BR on 17/11/9.
 *
 * @author <a href="mailto:wangjiacheng@szunicom.com">Jiacheng Wang</a>
 */
@SuppressWarnings("unused")
public class DefaultSgipAsyncPduResponse implements SgipAsyncPduResponse {

	private final WindowFuture<Integer, SgipPduRequest, SgipPduResponse> future;

	public DefaultSgipAsyncPduResponse(WindowFuture<Integer, SgipPduRequest, SgipPduResponse> future) {
		this.future = future;
	}

	@Override
	public SgipPduRequest getRequest() {
		return future.getRequest();
	}

	@Override
	public SgipPduResponse getResponse() {
		return future.getResponse();
	}

	@Override
	public int getWindowSize() {
		return future.getWindowSize();
	}

	@Override
	public long getWindowWaitTime() {
		return future.getOfferToAcceptTime();
	}

	@Override
	public long getResponseTime() {
		return future.getAcceptToDoneTime();
	}

	@Override
	public long getEstimatedProcessingTime() {
		long responseTime = getResponseTime();
		if (responseTime == 0 || future.getWindowSize() == 0) {
			return 0;
		}
		return (responseTime / future.getWindowSize());
	}

	@Override
	public String toString() {
		//noinspection StringBufferReplaceableByString
		StringBuilder builder = new StringBuilder();
		builder.append("sgip_async_resp: sequenceNumber [0x");
		builder.append(HexUtil.toHexString(future.getKey()));
		builder.append("] windowSize [");
		builder.append(getWindowSize());
		builder.append("] windowWaitTime [");
		builder.append(getWindowWaitTime());
		builder.append(" ms] responseTime [");
		builder.append(getResponseTime());
		builder.append(" ms] estProcessingTime [");
		builder.append(getEstimatedProcessingTime());
		builder.append(" ms] reqType [");
		builder.append(getRequest().getName());
		builder.append("] respType [");
		builder.append(getResponse().getName());
		builder.append("]");
		return builder.toString();
	}
}
